package huawei

import (
	"encoding/json"
	"log"
	"os"
)

type RuleTop struct {
	Rule []*Rule `json:"rule"`
}

type Rule struct {
	Pattern []string            `json:"pattern"`
	YangPath string             `json:"yang-path"`
	Key *RuleKey                `json:"key"`
	Parameters []*RuleParameter `json:"parameters"`
	Children []*Rule            `json:"children"`
}

type RuleKey struct {
	Index int32 `json:"index"`
	Pattern string `json:"pattern"`
}

type RuleParameter struct {
	Index int32 `json:"index"`
	Pattern string `json:"pattern"`
	YangPath string `json:"yang-path"`
}

func ParseRule(filePath string) (*Rule, error) {
	var ruleTop RuleTop

	content, err := os.ReadFile(filePath)
	if err != nil {
		return nil, err
	}

	// log.Printf("%s", content)

	if err := json.Unmarshal(content, &ruleTop); err != nil {
		log.Printf("json.Unmarshal failed. %s", err)
		return nil, err
	}

	rule := &Rule{
		Children: ruleTop.Rule,
	}
	return rule, nil
}
